package com.reliable.service.controller;

import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.reliable.api.dto.ReliableTransactionRecord.RiskTransactionDTO;
import com.reliable.api.dto.transaction.BankCardTransactionDTO;
import com.reliable.api.param.IdParam;
import com.reliable.api.param.ReliableTransactionRecord.ReliableTransactionRecordPageParam;
import com.reliable.common.constant.HttpStatus;
import com.reliable.common.core.domain.AjaxResult;
import com.reliable.common.core.page.TableDataInfo;
import com.reliable.common.utils.StringUtils;
import com.reliable.api.dto.ReliablePushTemplate.ReliableTransactionRecordDTO;
import com.reliable.service.domain.ReliabUploadRecord;
import com.reliable.service.utils.IdEncryptUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.reliable.service.domain.ReliableTransactionRecord;
import com.reliable.service.service.IReliableTransactionRecordService;

/**
 * 交易记录Controller
 * 
 * @author ruoyi
 * @date 2025-10-30
 */
@RestController
@RequestMapping("/reliable/transactionRecord")
@Api(value = "交易记录控制器", tags = {"交易记录管理"})
public class ReliableTransactionRecordController {

    @Autowired
    private IReliableTransactionRecordService reliableTransactionRecordService;

    /**
     * 查询交易记录列表
     */
    @ApiOperation("查询交易记录列表")
    @PostMapping("/list")
    public AjaxResult list(@RequestBody ReliableTransactionRecordPageParam pageParam) {

        IPage<ReliableTransactionRecord> page = new Page<>(pageParam.getPageNum(), pageParam.getPageSize());

        LambdaQueryWrapper<ReliableTransactionRecord> queryWrapper = new LambdaQueryWrapper<>();
        // sessionId精确匹配
        if (StringUtils.isNotEmpty(pageParam.getSessionId())) {
            String sessionId = pageParam.getSessionId();
            queryWrapper.eq(StringUtils.isNotEmpty(sessionId),
                    ReliableTransactionRecord::getSessionId, sessionId);
        }

        if (StringUtils.isNotEmpty(pageParam.getTaskId()))
        {
            queryWrapper.eq(ReliableTransactionRecord::getTaskId, IdEncryptUtils.decrypt(pageParam.getTaskId()));
        }


        // accountType精确匹配
        queryWrapper.eq(pageParam.getAccountType() != null && !pageParam.getAccountType().isEmpty(),
                         ReliableTransactionRecord::getAccountType, pageParam.getAccountType());
        // platformName模糊查询
        queryWrapper.like(pageParam.getPlatformName() != null && !pageParam.getPlatformName().isEmpty(),
                         ReliableTransactionRecord::getPlatformName, pageParam.getPlatformName());
        // create_time时间范围查询
        queryWrapper.ge(pageParam.getBeginTime() != null && StringUtils.isNotEmpty(pageParam.getBeginTime()), ReliableTransactionRecord::getCreateTime, pageParam.getBeginTime());
        queryWrapper.le(pageParam.getEndTime() != null && StringUtils.isNotEmpty(pageParam.getEndTime()), ReliableTransactionRecord::getCreateTime, pageParam.getEndTime());
        
        IPage<ReliableTransactionRecord> pageResult = reliableTransactionRecordService.page(page, queryWrapper);
        List<ReliableTransactionRecord> records = pageResult.getRecords();
        List<ReliableTransactionRecordDTO> list = new ArrayList<>();
        records.forEach(record -> {
            ReliableTransactionRecordDTO reliableTransactionRecordDTO = new ReliableTransactionRecordDTO();
            BeanUtils.copyProperties(record, reliableTransactionRecordDTO);
            reliableTransactionRecordDTO.setSessionId(record.getSessionId());
            reliableTransactionRecordDTO.setTransactionId(record.getTransactionId());
            list.add(reliableTransactionRecordDTO);
        });
        TableDataInfo data = new TableDataInfo(list, (int) pageResult.getTotal());
        return AjaxResult.success(data);
    }

    @ApiOperation("查询交易记录数量")
    @PostMapping("/getCount")
    public AjaxResult getCount(){
        return AjaxResult.success(reliableTransactionRecordService.getTransactionStatistics());
    }

    @ApiOperation("查询交易记录详情")
    @PostMapping("/info")
    public AjaxResult info(@RequestBody IdParam param){
        String transactionId = param.getBusinessId();
        if (transactionId == null) return AjaxResult.error("参数错误");
        ReliableTransactionRecord byId = reliableTransactionRecordService.getById(transactionId);
        byId.setDelFlag( null);
        byId.setTransactionId(byId.getTransactionId());
        byId.setSessionId(byId.getSessionId());
        byId.setTaskId( null);
        return AjaxResult.success(byId);
    }
    
    @ApiOperation("查询风险交易记录")
    @PostMapping("/getRiskTransactions")
    public AjaxResult getRiskTransactions(){
        List<RiskTransactionDTO> riskTransactions = reliableTransactionRecordService.getRiskTransactions();
        return AjaxResult.success(riskTransactions);
    }


    /**
     * 响应返回结果
     *
     * @param rows 影响行数
     * @return 操作结果
     */
    protected AjaxResult toAjax(boolean rows) {
        return rows ? AjaxResult.success() : AjaxResult.error();
    }


    /**
     * 银行卡数据列表
     */
    @PostMapping("/bankCardList")
    @ApiOperation("银行卡数据列表")
    public AjaxResult bankCardList(@RequestBody ReliableTransactionRecordPageParam param) {
        // 设置默认值
        if (param.getPageNum() == null) {
            param.setPageNum(1L);
        }
        if (param.getPageSize() == null) {
            param.setPageSize(10L);
        }

        // 查询数据
        List<BankCardTransactionDTO> bankCards = reliableTransactionRecordService.selectBankCardTransactions(
                param.getAccountIdentifier(),
                param.getPlatformName(),
                param.getPageNum().intValue(),
                param.getPageSize().intValue()
        );

        // 构造分页结果
        TableDataInfo dataTable = new TableDataInfo();
        dataTable.setCode(HttpStatus.SUCCESS);
        dataTable.setMsg("查询成功");
        dataTable.setRows(bankCards);
        dataTable.setTotal((long) bankCards.size());

        return AjaxResult.success(dataTable);
    }
}